﻿//@ Shweta
//
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Data;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentManager
{
    public partial class Approve_RejectRequisition : System.Web.UI.Page
    {
        static int a = 0;
        List<Requisition> reqList;
        ApproveRequisitionController appReqCtrl = new ApproveRequisitionController();
        Employee employee = new Employee();
        int reqFormId;
        string empName;
        Requisition remark, status;
       
        //static Requisition r;
        protected void Page_Load(object sender, EventArgs e)
        {  
            employee=(Employee)Session["CurrentUser"];
       
           
                if (!IsPostBack)
                {
                    Bind();

                    if (gv_ReqApproveList.Rows.Count==0)
                    {
                        lbl_message.Visible = true;
                        lbl_message.Text = "There is no requisition available for Approval";
                       
                    }
                    
                }
          
           
        }

        protected void Bind()
        {
            string deptId = employee.DeptId;
            reqList = appReqCtrl.getRequisitionByDeptIdAndAppliedStatus(deptId);
            
            gv_ReqApproveList.DataSource = reqList;
            gv_ReqApproveList.DataBind();
            
        }
        protected void gv_ReqApproveList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gv_ReqApproveList.PageIndex = e.NewPageIndex;

                //Bind data to the GridView control.
                Bind();
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('there is no requisition available or data base error!');</script>");

            }
        }

          protected void gv_OnRowCancelEdit(object sender, GridViewCancelEditEventArgs e)
        {


        }


         
        protected void gv_ReqApproveList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HiddenField hdn2;
            HiddenField hdn1;
                 
                if (String.Equals(e.CommandName, "ViewDetails"))
                {
                 
                    reqFormId = int.Parse(gv_ReqApproveList.DataKeys[0].Value.ToString());
                    empName = gv_ReqApproveList.DataKeys[0].Value.ToString();
                    Panel1.Visible = true;
                  
                    int index = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = gv_ReqApproveList.Rows[index];
                     hdn1 = (HiddenField)row.FindControl("hdnEmpId");
                    hdn2 = (HiddenField)row.FindControl("hdnReqId");
                         
                    txt_ReqID.Text = hdn2.Value.ToString();
                    a = Convert.ToInt32(hdn2.Value.ToString());
                    Employee emp = appReqCtrl.selectEmployee(hdn1.Value);
                    txt_EmpName.Text = emp.EmpName.ToString();
                    txt_Email.Text = emp.Email.ToString();
                    hdnempId.Value = emp.Email;//
                    reqList = appReqCtrl.getRequistion();
                    foreach (Requisition r in reqList)
                    {
                        if (r.EmpId == emp.EmpId)
                        {
                            txt_ReqDate.Text = r.RequestedDate.ToString();
                        }
                    
                    }
                    List<RequisitionItems> ReqItems = appReqCtrl.getRequisitionItemsbyReqFormId(Convert.ToInt32(hdn2.Value));

                    gv_ReqDetailList.DataSource = ReqItems;
                      gv_ReqDetailList.DataBind();
                }
        }

        

        protected void btn_approve_Click(object sender, EventArgs e)
        {
            //try
            //{
                reqFormId = Convert.ToInt32(txt_ReqID.Text);
                Requisition r = appReqCtrl.selectRequisition(reqFormId);
                r.Status = "Approved";
                r.ApprovedOn = DateTime.Now.ToString("yyyy-MM-dd");
                appReqCtrl.setReqStatus(r, reqFormId);

                string To = hdnempId.Value.ToString();
                Label1.Text = To;
                Email em = new Email(r.Status, To);
                em.createMail();

                Response.Redirect("Approve_RejectRequisition.aspx");         
               
               //Employee emp = new Employee();
               //if (emp.EmpId == r.EmpId)
               //{
               //    string To = emp.Email.ToString();
               //    Label1.Text = To;
               //    Email em = new Email(r.Status, To);
               //    em.createMail();
               //}
               //else
               //    Label2.Text = "Not valid";


            //}
            //catch (Exception ex)
            //{
            //    Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('there is data base error!');</script>");
            //}
        }

        protected void btn_reject_Click(object sender, EventArgs e)
        {
            //try
            //{ 
                reqFormId = Convert.ToInt32(txt_ReqID.Text);
                Requisition r = appReqCtrl.selectRequisition(reqFormId);
                r.Status = "Rejected";
                r.Remarks = txt_rejectReason.Text;
                appReqCtrl.setReqStatus(r, reqFormId);
                Panel1.Visible = false;

                string To = hdnempId.Value.ToString();
                Label1.Text = To;
                Email em = new Email(r.Status, To);
                em.createMail();

                Response.Redirect("Approve_RejectRequisition.aspx");
             
            //}
            //catch (Exception ex)
            //{
            //    Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('there is data base error!');</script>");
            //}
        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void gv_ReqDetailList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gv_ReqApproveList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txt_ReqID_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btn_approve_Command(object sender, CommandEventArgs e)
        {
           
        }

    }
}